package leetcode

func lengthOfLongestSubstringMethod1(s string) int {
	n := len(s)
	if n < 2 {
		return n
	}

	table := map[uint8]int{}
	left, maxlength := 0, 0

	for i := 0; i < n; i++ {

		if c, ok := table[(s[i])]; ok {
			if i-left > maxlength {
				maxlength = i - left
			}
			for a := left; a <= c; a++ {
				delete(table, s[a])
			}

			left = c + 1
		}
		table[s[i]] = i

	}
	if n-left > maxlength {
		maxlength = n - left
	}

	return maxlength
}

func lengthOfLongestSubstringMethod2(s string) int {
	n := len(s)
	if n < 2 {
		return n
	}
	maxlength, left, right := 1, 0, 0
	table := map[uint8]int{}

	for right < n {
		if c, ok := table[s[right]]; ok && c >= left {
			left = c + 1
		}
		table[s[right]] = right
		currentLength := right - left + 1

		if currentLength > maxlength {
			maxlength = currentLength
		}
		right++
	}

	return maxlength
}

func lengthOfLongestSubstringMethod3(s string) int {
	n := len(s)
	if n < 2 {
		return n
	}

	maxlength, left, right := 1, 0, 0
	table := [128]bool{}

	for left < n {
		if right < n && !table[s[right]] {
			table[s[right]] = true
			right++
		} else {
			table[s[left]] = false
			left++
		}

		if right-left > maxlength {
			maxlength = right - left
		}
	}

	return maxlength
}
